﻿// JavaScript Document

//
/*
* 图片自动播放js
* html结构：
*	<div class="homeAutoPlayWrap">
*		<img src="../images_3/home/img01.jpg" />
*		<img src="../images_3/home/img02.jpg" />
*		<img src="../images_3/home/topBg.jpg" />
*	</div>
*
* 调用方法：
*	$('.homeAutoPlayWrap').homeAutoPlay();
*
*
* 参数说明：
* labelStyle:dot|Num  序号样式
* 
* dot 右中部圆点(默认)
* Num 右下角序号
* 
*/

var  startPlay = null;
(function($) {       
    $.fn.homeAutoPlay = function(options) {       
        var opts = $.extend({}, $.fn.homeAutoPlay.defaults, options);      
        return this.each(function(i) { 
            $this = $(this);    
            var _img = $this.find('a');
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts; 
            if($this.find('.'+o.labelStyle+'AutoPlayLabel').length==0){
                $this.append('<div class="'+o.labelStyle+'AutoPlayLabel"></div>');
                _img.each(function(i){
                    if(o.labelStyle == 'dot'){
                    	$this.find('.'+o.labelStyle+'AutoPlayLabel').append('<span></span>');
                    }else{
                    	$this.find('.'+o.labelStyle+'AutoPlayLabel').append('<span>'+(i+1)+'</span>');   
                    };
                 });
                
                $this.find('.'+o.labelStyle+'AutoPlayLabel').find('span:first').addClass('over');
                $this.find('a:first').addClass('active').fadeIn("slow");
                if(startPlay)
                	clearInterval(startPlay);
                startPlay = setInterval(o.autoPlay, 5000); 
            }
                         
            $this.find('.'+o.labelStyle+'AutoPlayLabel').find('span').click(function(){
                    clearInterval(startPlay);
                    $(this).addClass('over').siblings().removeClass('over');
                    _img.removeClass('active');
                    $(_img.get($(this).index())).addClass('active').fadeIn('slow',function(){
                        _img.not('.active').fadeOut('slow');
                    }); 
                    startPlay = setInterval(o.autoPlay, 5000);  
                }); //click End   
           // }
        });     
    };    
    $.fn.homeAutoPlay.defaults = {
        labelStyle:'dot',
        autoPlay:function(para){
            var nextLi; 
            $this.find('.active + a').is("a")?nextLi = $this.find('.active + a'):nextLi = $this.find('a:first');
            $this.find('a').removeClass('active');
            nextLi.addClass('active').fadeIn('slow',function(){
                $this.find('a').not('.active').fadeOut('slow');
            }); 
            $($this.find('span').get($this.find('.active').index())).addClass('over').siblings().removeClass('over');
        }//function End
        
    };           
})(jQuery);